<?php
/**
 * Created by PhpStorm.
 * User: 杨永安
 * Date: 2017/4/18
 * Time: 15:39
 */

namespace app\api\controller;


use app\api\model\Order;
use think\Controller;

class Master extends Controller
{

    public function _initialize()
    {
        if (!isMaster()) {
            $this->error("您没有店铺");
        }

    }

    function my_shop_info()
    {
        if (isMaster()) {
            $res['msg'] = db('shop')->where("id", isMaster())->find();
            $res['code'] = 1;
        } else {

            $res['msg'] = "您没有店铺";
            $res['code'] = -1;
        }
        return json($res);
    }


    function order_list($offset = 0, $status = 'all')
    {
        $order = new Order();

        if ($status == 'all') {
            $res['msg'] = $order->where('ShopId', isMaster())->limit($offset, 10)->order('Id', 'DESC')->select();
        } else {
            $res['msg'] = $order->where('ShopId', isMaster())->limit($offset, 10)->order('Id', 'DESC')->where('Status', $status)->select();
        }

        foreach ($res['msg'] as $k => $v) {
            $res['msg'][$k]['Name'] = $v->address()['Name'];
            $res['msg'][$k]['Ad'] = $v->address()['Address'];
            $res['msg'][$k]['cost'] = $v->cost($v['Id']);
        }

        $res['code'] = 1;

        return json($res);
    }

    function done($order_id = 0)
    {
        if ($order_id) {
            $order = new Order();
            $order = $order->where("Id", $order_id)->find();
            if ($order) {
                if ($order['ShopId'] == isMaster()) {
                    $set = db('order')->where("Id", $order_id)->update(['Status' => 3]);
                    if ($set) {
                        $res['msg'] = "操作成功";
                        $res['code'] = -3;
                        roomMsg([
                            "content" => "新配送订单",
                            'extra' => "NEW_SHIP"
                        ], 'shiper');
                    } else {
                        $res['msg'] = "操作失败";
                        $res['code'] = -4;
                    }
                } else {
                    $res['msg'] = "订单不是您的店铺的";
                    $res['code'] = -3;
                }
            } else {
                $res['msg'] = "订单不存在";
                $res['code'] = -2;
            }
        } else {
            $res['msg'] = "订单号不正确";
            $res['code'] = -1;
        }
        return json($res);
    }
}